<template>
  <div class="book-detail">
    <t-card :bordered="false" class="content-card">
      <template #title>
        <t-space size="18px">
          <h2 class="card-title">入库订单表</h2>
          <icon-font name="chevron-down-circle" />
          <t-link class="edit-btn-color" hover="color">编辑</t-link>
        </t-space>
      </template>
      <template #actions>
        <t-space size="8px">
          <t-button @click="handleWarehousing" class="add-btn">
            <p class="flex align-center">
              <i class="iconfont icon-warehousing" style="font-size: 24px;margin-right: 4px"></i>
              入库
            </p>
          </t-button>
        </t-space>
      </template>

      <div class="filter ">
        <t-space size="12px" class="flex-5">
          <t-select style="width: 140px;" v-model="params.classify_id" :options="classifyOptions"
                    placeholder="按产品分类">
            <template #label>
              <icon-font name="layers" style="color: #000;"/>
            </template>
          </t-select>
          <t-select style="width: 140px;" v-model="params.brand_id" :options="brandOptions" placeholder="按产品品牌">
            <template #label>
              <icon-font name="layers" style="color: #000;"/>
            </template>
          </t-select>
          <t-select style="width: 140px;" v-model="params.brand_id" :options="brandOptions" placeholder="按产品机型">
            <template #label>
              <icon-font name="layers" style="color: #000;"/>
            </template>
          </t-select>
          <t-input style="width: 220px;" v-model="params.key" placeholder="输入产品名称、关键字、ID">
            <template #label>
              <icon-font name="search" size="16px" style="color:#000;font-weight: bold"/>
            </template>
          </t-input>
        </t-space>
      </div>

      <div class="pt16">
        <t-table
          rowKey="id"
          :data="lists"
          :columns="columns"
          :stripe="stripe"
          :bordered="bordered"
          :hover="hover"
          :size="size"
          :max-height="maxHeight"
          :table-layout="tableLayout ? 'auto' : 'fixed'"
          :showHeader="showHeader"
          cellEmptyContent="-"
          resizable
          :loading="loading"
          @page-change="pageChange"
          :pagination="pagination"
          :selected-row-keys="selectedRowKeys"
          @select-change="handleSelectChange"
        >
          <template #title-slot-option></template>
          <template #thumb="{row}">
            <t-image-viewer :images="[row.thumb]" :closeOnEscKeydown="false">
              <template #trigger="{ open }">
                <div class="tdesign-demo-image-viewer__ui-image" @click="open">
                  <p class="prod-img">
                    <!--<img alt="test" :src="row.thumb"/>-->
                    <img alt="图片" src="@/assets/product.png" />
                  </p>
                  <div class="tdesign-demo-image-viewer__ui-image--hover">
                    <span><browse-icon size="1.4em"/> 预览</span>
                  </div>
                </div>
              </template>
            </t-image-viewer>
          </template>
          <template #option="{row}">
            <t-space size="12px">
              <t-link theme="primary" class="edit-btn-color" hover="color">流转日志</t-link>
              <t-link theme="warning" class="edit-btn-color" hover="color">详情</t-link>
            </t-space>
          </template>
        </t-table>
      </div>
    </t-card>
    <div v-if="selectedRowKeys?.length" class="footer">
      <t-space size="12px">
        <t-popconfirm theme="danger" confirm-btn="删除">
          <template #content>
            <div>
              <h3 class="popconfirm-title">确认批量下架？</h3>
            </div>
          </template>
          <t-button theme="default" variant="outline">下架</t-button>
        </t-popconfirm>
        <t-button theme="default" variant="outline">上架</t-button>
      </t-space>
      <t-space size="12px">
        <p class="flex align-center">库存总数：<span class="expenditure">820</span></p>
        <p class="flex align-center">已出库：<span class="income">290</span></p>
      </t-space>
    </div>

    <product-add ref="add" @refresh="getData"></product-add>
  </div>
</template>

<script lang="jsx">
import {IconFont, CalendarIcon, BrowseIcon} from "tdesign-icons-vue";
import IconUAV from "@/components/icon-UAV/index.vue";
import ProductAdd from "@/pages/product/warehousing/add/index.vue";
import {productDeleteApi, productListApi} from "@/api/product";
import {classifyListApi} from "@/api/classify";

const initialData = [];
const total = 500;
for (let i = 0; i < total; i++) {
  initialData.push({
    id: i + 1,
    thumb: '',
    title: 'DJI Mavic 3 Pro Cine 大师套装（DJI RC Pro 遥控器）',
    price: '¥2438.00',
    num: 100,
    warehouse: '总仓',
    time: '2021-09-01 12:00:00',
    operator: '张三',
  });
}

export default {
  name: 'Warehousing',
  components: {IconUAV, IconFont, BrowseIcon, ProductAdd},
  data() {
    return {
      renderPrefixIcon: () => <CalendarIcon/>,
      params: {
        page: 1,  // 当前页
        limit: 10,  // 每页条数
        is_del: '', // 是否删除（1回收站；0正常）
        classify_id: '', // 分类
        brand_id: '', // 品牌
        hot_level: '',  // 推荐等级（5~1）
        key: '',  // 关键字
        status: 'all', // 状态
      },
      tabList: [
        {label: '全部', num: 564, value: 'all'},
        {label: '上架', num: 564, value: 'selling'},
        {label: '下架', num: 564, value: 'warehouse'},
        {label: '回收', num: 564, value: 'recycle'},
      ],  //
      lists: initialData,
      size: 'medium',
      tableLayout: false,
      stripe: false,
      bordered: false,
      hover: true,
      showHeader: true,
      columns: [
        {colKey: 'row-select', type: 'multiple', width: 40,},
        {colKey: 'thumb', title: '图片', align: 'center', width: 60,},
        {colKey: 'title', title: '产品名称', width: 240, ellipsis: true},
        {colKey: 'price', title: '进货价', width: 120},
        {colKey: 'num', title: '库存数量', width: 90},
        {colKey: 'warehouse', title: '仓库', width: 140},
        {colKey: 'time', title: '入库时间', width: 140},
        {colKey: 'operator', title: '操作人', width: 100},
        {colKey: 'option', title: 'title-slot-option', fixed: 'right', width: 100},
      ],
      selectedRowKeys: [],
      pagination: {
        defaultCurrent: 1,
        defaultPageSize: 20,
        total: 0,
      },
      maxHeight: 472,
      loading: false,

      classifyOptions: [],
      brandOptions: [],
      levelOptions: [
        {label: '5级', value: 5},
        {label: '4级', value: 4},
        {label: '3级', value: 3},
        {label: '2级', value: 2},
        {label: '1级', value: 1},
      ],
    };
  },
  computed: {
    //...mapState('book', ['bookId'])
  },
  mounted() {
    let height = document.documentElement.clientHeight - 244;
    this.maxHeight = height
    window.onresize = () => {
      // 获取屏幕高度 - 顶部高度
      height = document.documentElement.clientHeight - 244;
      this.maxHeight = height
    };

    //this.getData()
    //this.getOptions()
  },
  methods: {
    // 入库
    handleWarehousing() {
      this.$refs.add.open()
    },
    // 获取列表
    getOptions() {
      const params = {
        page: 1,  // 当前页
        limit: 9999,  // 每页条数
        // 模块类型（如产品分类 product_classify，说明 product_brand，发货说明 product_deliver，保障说明 product_safeguard ...）
      }
      classifyListApi({...params, belong_module: 'product_classify'}).then(res => {
        this.classifyOptions = res.data.map(item => ({label: item.title, value: item.id}))
      })
      classifyListApi({...params, belong_module: 'product_brand'}).then(res => {
        this.brandOptions = res.data.map(item => ({label: item.title, value: item.id}))
      })
    },
    // 添加
    goProductAdd() {
      this.$refs.add.open('add');
    },
    // 编辑
    handleEdit(row) {
      this.$refs.add.open('edit', row)
    },
    // 复制
    handleCopy(row) {
      this.$refs.add.open('copy', row)
    },
    // 删除
    handleDelete(evt, row) {
      productDeleteApi({id: row.id}).then(res => {
        this.$message.success(res.retmsg)
        this.getList()
      }).catch(err => {
        console.log('err', err)
      })
    },
    // 获取数据
    getData() {
      this.pagination.defaultCurrent = 1
      this.getList()
    },
    // 获取列表
    getList() {
      this.loading = true
      this.params.page = this.pagination.defaultCurrent
      productListApi(this.params).then(res => {
        this.pagination.total = res.count
        this.lists = res.data
      }).catch(err => {
      }).finally(() => {
        this.loading = false
      })
    },
    // 切换状态
    changeTab(tab) {
      this.params.user = tab.value
    },
    // 切换分页
    pageChange(pageInfo) {
      const {current, defaultCurrent, defaultPageSize,} = pageInfo
      this.pagination.defaultCurrent = current
      this.getList()
    },
    handleSelectChange(value, {selectedRowData}) {
      this.selectedRowKeys = value;
      console.log(value);
      console.log(selectedRowData);
    },
  },
}
</script>

<style lang="less" scoped>
.book-detail {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: var(--td-radius-medium);
  background-color: #FFFFFF;
}

/deep/ .content-card {

  .t-card__header {
    padding: var(--td-comp-paddingTB-xl) var(--td-comp-paddingLR-xl);
    padding-bottom: 0;
  }

  .t-card__body {
    padding: var(--td-comp-paddingTB-l) var(--td-comp-paddingLR-xl) var(--td-comp-paddingTB-m);
  }

  .setting-btn {
    font-size: 14px;
    font-weight: 400;
    line-height: 19.6px;
    text-align: left;
    color: #005687;
  }

  .invite-btn {
    font-size: 12px;
    font-weight: 400;
    line-height: 16.8px;
    text-align: left;
    color: #005687;

    background-color: #F3FBFF;
    border: 1px solid #005687;
    border-radius: var(--td-radius-default);
  }
}

/deep/ .action-btn {
  width: 126px;
  height: 42px;
  font-size: 16px;
  font-weight: 600;
  line-height: 22.4px;
  text-align: left;
  color: #FFFFFF;

  .t-button__text {
    display: flex;
    align-items: center;
    justify-content: center;

    i {
      z-index: 2;
      font-size: 12px;
      font-weight: bold;
      margin-right: 6px;

      &:after {
        position: absolute;
        display: inline-block;
        content: '';
        top: 50%;
        left: 50%;
        width: 14px;
        height: 14px;
        z-index: -1;
        background-color: #FFFFFF;
        border-radius: var(--td-radius-round);
        transform: translateX(-50%) translateY(-50%);
      }
    }
  }
}

.action-btn1 {
  background-color: #14A54E;
  border: 1px solid #14A54E;

  i {
    color: #14A54E;
  }
}

.action-btn2 {
  background-color: #FF5C46;
  border: 1px solid #DFE3E7;
}

.action-btn3 {
  background-color: #565656;
  border: 1px solid #DFE3E7;
}

.action-btn4 {
  color: black;
  background-color: #F9F9F9;
  border: 1px solid #E1E1E1;
}

/deep/ .filter {
  padding-top: 13px;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .t-input, .t-input.t-is-readonly {
    background-color: #F8F8F8;
  }
}

//.import-btn {
//  width: 69px;
//  color: #000000;
//  font-size: 14px;
//  font-weight: 400;
//  line-height: 19.6px;
//  background-color: #F9F9F9;
//  border: 1px solid #E1E1E1;
//}

.income {
  color: #14A54E;
  display: flex;
  align-items: center;

  i {
    width: 17px;
    height: 17px;
    color: #FFFFFF;
    font-size: 12px;
    display: flex;
    margin-left: 8px;
    align-items: center;
    justify-content: center;
    background-color: #14A54E;
    border-radius: var(--td-radius-small);
  }
}

.expenditure {
  color: #FF5C46;
  display: flex;
  align-items: center;

  i {
    width: 17px;
    height: 17px;
    color: #FFFFFF;
    font-size: 12px;
    display: flex;
    margin-left: 8px;
    align-items: center;
    justify-content: center;
    background-color: #FF5C46;
    border-radius: var(--td-radius-small);
  }
}

.footer {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #F9F9F9;
  padding: var(--td-comp-paddingTB-m) var(--td-comp-paddingLR-xl);
  padding-right: var(--td-comp-paddingLR-xxl);
}
</style>
